<?php

namespace App\model;

use App\User;
use Illuminate\Database\Eloquent\Model;

/**
 * App\model\Coustomer
 *
 * @property int $customer_id
 * @property string|null $mobile
 * @property string|null $company_name
 * @property int|null $industry_id
 * @property int|null $project_id
 * @property int|null $source_id
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereCompanyName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereCustomerId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereIndustryId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereMobile($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereProjectId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereSourceId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property string|null $name
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereName($value)
 * @property int|null $user_id
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereUserId($value)
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\model\CustomerFollow[] $follow
 * @property-read \App\model\Industry|null $industry
 * @property-read \App\model\Project|null $project
 * @property-read \App\model\Source|null $source
 * @property-read \App\User|null $user
 * @property-read \App\model\CustomerFollow $beizhu
 * @property int|null $demand_id
 * @property int|null $area_id
 * @property string|null $city1
 * @property string|null $city2
 * @property-read \App\model\Area|null $area
 * @property-read \App\model\CoustomerDemand|null $demand
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereAreaId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereCity1($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereCity2($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereDemandId($value)
 * @property string|null $status 状态
 * @property int|null $writer_id 推广员的id
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereWriterId($value)
 * @property-read mixed $status_desc
 * @property int|null $customer_level_id 客户等级id
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereCustomerLevelId($value)
 * @property string|null $wechat 微信
 * @property string|null $tel 固话
 * @property string|null $company_address 公司地址
 * @property-read \App\model\CustomerLevel|null $level
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereCompanyAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereTel($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereWechat($value)
 * @property string $follow_status
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereFollowStatus($value)
 * @property-read mixed $follow_status_desc
 * @property string|null $sex
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\model\CustomerSign[] $sign
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereSex($value)
 * @property-read \App\model\Project|null $projects
 * @property string|null $adv_name
 * @property string|null $external_url
 * @property string|null $clue_id
 * @property int|null $is_sea 是否是公海客户
 * @property int|null $receive_id 记录领取的customer_id
 * @property string|null $next_time 下次领取时间

 * @property-read mixed $tuiguang
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\User[] $sea

 * @property-read \App\model\CoustomerDemandCopy|null $zhaoshangDemand
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereAdvName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereClueId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereExternalUrl($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereIsSea($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereNextTime($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\model\Coustomer whereReceiveId($value)
 */
class Coustomer extends Model
{
    protected $table='customer';
    protected $primaryKey='customer_id';
    const Follow_up='Follow_up'; //跟进中
    const No_Follow_Up='No_Follow_Up';//未跟进
    const To_Be_Follow_up='To_Be_Follow_up';
    const Completed='Completed';
    const Unintentional='Unintentional';
    const Man=1;
    const WOMAN=2;
    public static $statusMap=[
        self::Follow_up=>'跟进中',
        self::No_Follow_Up=>'未分配',
        self::Completed=>'已成交',
    ] ;
    public static $followStatusMap=[
        self::To_Be_Follow_up=>'已到访',
        self::Completed=>'已成交',
        self::Unintentional=>'无意向',
    ];
    protected $appends=[
        'statusDesc','follow_status_desc','tuiguang'
    ];
    public function industry(){
       return  $this->belongsTo(Industry::class,'industry_id','industry_id');
    }
    public function project(){
        return $this->belongsTo(Project::class,'project_id','project_id');
    }
    public function projects(){
        return $this->belongsTo(Project::class,'project_id','project_id');
    }
    public function source(){
        return $this->belongsTo(Source::class,'source_id','source_id');
    }

    public function demand(){

            return $this->belongsTo(CoustomerDemand::class,'demand_id');


    }
    public function zhaoshangDemand(){
        return $this->belongsTo(CoustomerDemandCopy::class,'demand_id');

    }
    public function area(){

        return $this->belongsTo(Area::class,'area_id');
    }
    public function follow(){
        return $this->hasMany(CustomerFollow::class,'customer_id');
    }
    public function beizhu(){
        return $this->hasOne(CustomerFollow::class,'customer_id');
    }
    public function user(){
        return $this->belongsTo(User::class,'user_id');
    }
    public function level(){
        return $this->belongsTo(CustomerLevel::class,'customer_level_id','customer_level_id');
    }
    public function sea(){
        return $this->belongsToMany(User::class,'sea_customer','customer_id','user_id');
    }
    //顾客标签
    public function sign(){
    return $this->belongsToMany(CustomerSign::class,'customer_zhongjian_sign','customer_id','sign_id');
}
    public function getStatusDescAttribute($value)
    {
        $status=$this->attributes['status'] ?? '';
        return self::$statusMap[$status] ?? '无状态';
    }
    public function getFollowStatusDescAttribute($value){
       $status= $this->attributes['follow_status'] ?? '';
        return self::$followStatusMap[$status] ?? '无状态';
    }
    public function getTuiguangAttribute($value){

        return [
                'name'=>'推广页面',
                'url'=>'https://www.baidu.com/',
        ];
    }
}
